using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CustomSecurity;
namespace ListForms
{
    public partial class FrmRoleFunction : BasicForms.FrmBase
    {
        Role _Role = null;
        Function _Function = null;
        Right _Right;

        public FrmRoleFunction(Role role, Function function, Right right)
        {
            _Role = role;
            _Function = function;
            _Right = right;

            InitializeComponent();
        }

        private void FrmRoleFunction_Load(object sender, EventArgs e)
        {
            if (DesignMode) return;
            txtFunction.Text = _Function.Name;
            txtRole.Text = _Role.Name;

            chkRead.Visible = (_Function.PosibleRight & CustomSecurity.Right.Read) > 0;
            chkRead.Checked = (_Right & CustomSecurity.Right.Read) > 0;

            chkWrite.Visible = (_Function.PosibleRight & CustomSecurity.Right.Write) > 0;
            chkWrite.Checked = (_Right & CustomSecurity.Right.Write) > 0;

            chkExecute.Visible = (_Function.PosibleRight & CustomSecurity.Right.Execute) > 0;
            chkExecute.Checked = (_Right & CustomSecurity.Right.Execute) > 0;

            chkDelete.Visible = (_Function.PosibleRight & CustomSecurity.Right.Delete) > 0;
            chkDelete.Checked = (_Right & CustomSecurity.Right.Delete) > 0;

            chkAdvancedEdit.Visible = (_Function.PosibleRight & CustomSecurity.Right.Advancededit) > 0;
            chkAdvancedEdit.Checked = (_Right & CustomSecurity.Right.Advancededit) > 0;

            chkDuocSuaHDCTNguoiKhacTao.Visible = (_Function.PosibleRight & CustomSecurity.Right.ThaoTacVoiHoaDonChungTuDoNguoiKhacTao) > 0;
            chkDuocSuaHDCTNguoiKhacTao.Checked = (_Right & CustomSecurity.Right.ThaoTacVoiHoaDonChungTuDoNguoiKhacTao) > 0;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            CustomSecurity.Right right = CustomSecurity.Right.None;
            right = right | (chkRead.Visible && chkRead.Checked ? CustomSecurity.Right.Read : CustomSecurity.Right.None);
            right = right | (chkWrite.Visible && chkWrite.Checked ? CustomSecurity.Right.Write : CustomSecurity.Right.None);
            right = right | (chkExecute.Visible && chkExecute.Checked ? CustomSecurity.Right.Execute : CustomSecurity.Right.None);
            right = right | (chkDelete.Visible && chkDelete.Checked ? CustomSecurity.Right.Delete : CustomSecurity.Right.None);
            right = right | (chkAdvancedEdit.Visible && chkAdvancedEdit.Checked ? CustomSecurity.Right.Advancededit : CustomSecurity.Right.None);
            right = right | (chkDuocSuaHDCTNguoiKhacTao.Visible && chkDuocSuaHDCTNguoiKhacTao.Checked ? CustomSecurity.Right.ThaoTacVoiHoaDonChungTuDoNguoiKhacTao : CustomSecurity.Right.None);

            _Role.FunctionWithRights[_Function.ID] = right;
            foreach (Function childFunction in FunctionList.All.GetByParent(_Function.ID))
            {
                _Role.FunctionWithRights[childFunction.ID] = right;
            }
            _Role.UpdateFunctionWithRights();

            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}